package com.iurac.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.iurac.domain.Book;
import com.iurac.domain.DicType;
import com.iurac.domain.DicValue;
import com.iurac.exception.ManageException;
import com.iurac.service.BookService;
import com.iurac.service.DictionaryService;
import com.iurac.uitl.UUIDUtil;
import com.iurac.vo.BorrowVo;
import com.iurac.vo.DictionaryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.*;

@Controller
public class DictionaryController {

    @Autowired
    private DictionaryService dictionaryService;
    @Autowired
    private ServletContext servletContext;

    @RequestMapping("/dictionary/getDictionary.do")
    @ResponseBody
    public Map<String,Object> getDictionary(@RequestParam(value="page") int page,@RequestParam("limit") int limit) throws IOException {
        List<DictionaryVo> dictionaryVoList = dictionaryService.getDictionaryByCondition();
        Map<String,Object> map = new HashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",dictionaryVoList.size());
        map.put("data",dictionaryVoList);

        return  map;
    }

    @RequestMapping("/dictionary/deleteDictionaryByVid.do")
    @ResponseBody
    public Map<String,Object> deleteDictionaryByVid(@RequestParam(value="valueId") String valueId) throws IOException {
        boolean success;
        try {
            success = dictionaryService.deleteDictionaryByVid(valueId);
        } catch (ManageException e) {
            e.printStackTrace();
            success = false;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("success",success);

        return  map;
    }

    @RequestMapping("/dictionary/getDicType.do")
    @ResponseBody
    public Map<String,Object> getDicType() throws IOException {
        List<DicType> dicTypeList = dictionaryService.getDicType();
        Map<String, Object> map = new HashMap<>();
        map.put("dicTypeList",dicTypeList);

        return  map;
    }

    @RequestMapping("/dictionary/saveDicType.do")
    @ResponseBody
    public Map<String,Object> saveDicType(DicType dicType) throws IOException {
        dicType.setId(UUIDUtil.getUUID());
        boolean success;
        try {
            success = dictionaryService.saveDicType(dicType);
        } catch (ManageException e) {
            e.printStackTrace();
            success = false;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("success",success);

        return  map;
    }

    @RequestMapping("/dictionary/saveDicValue.do")
    @ResponseBody
    public Map<String,Object> saveDicValue(DicValue dicValue) throws IOException {
        dicValue.setId(UUIDUtil.getUUID());
        boolean success;
        try {
            success = dictionaryService.saveDicValue(dicValue);
        } catch (ManageException e) {
            e.printStackTrace();
            success = false;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("success",success);

        return  map;
    }

    @RequestMapping("/dictionary/updateDicValue.do")
    @ResponseBody
    public Map<String,Object> updateDicValue(DicValue dicValue) throws IOException {
        boolean success;
        try {
            success = dictionaryService.updateDicValue(dicValue);
        } catch (ManageException e) {
            e.printStackTrace();
            success = false;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("success",success);

        return  map;
    }

    @RequestMapping("/dictionary/reloadDictionary.do")
    @ResponseBody
    public void reloadDictionary() throws IOException {
        Map<String, List<DicValue>> dictionary = dictionaryService.getDictionary();
        Set<String> set = dictionary.keySet();

        for(String type: set){
            List<DicValue> dicValueList = dictionary.get(type);
            servletContext.setAttribute(type+"List",dicValueList);
        }
    }
}
